//
//  ApiClient.h
//  QFBAPP
//
//  Created by 无常先生 on 2018/5/2.
//  Copyright © 2018年 YIXun. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <AFNetworking/AFNetworking.h>
#import "ApiSeriverProtocol.h"

typedef void(^SuccessBlock)(NSURLSessionDataTask * task , id  responseBody);
typedef void(^OtherCodeBlock)(NSURLSessionDataTask * task , id  responseBody);
typedef void(^FailureBlock)(NSURLSessionDataTask * task , NSError * error);
typedef void(^ProgressBlock)(NSProgress * downloadProgress);
@interface ApiClient : NSObject
// 初始化一个网络请求对象。
- (instancetype _Nullable )initWithServer:(nullable NSObject<ApiSeriverProtocol> *)server;

@property (nonatomic, strong, nullable) NSObject<ApiSeriverProtocol> *server;

@property (nonatomic, strong, readonly) AFHTTPSessionManager * _Nullable sessionManager;
// 当前的任务
@property (nonatomic, strong, readonly) NSURLSessionDataTask *task;

// 交给子类实现。
- (AFHTTPRequestSerializer *_Nullable)clientHTTPHeaderField;
// 子类可以根据errorCode 做相应的操着
- (void)requestErrorWithErrorCode:(NSError *)error;
/** get请求 */
- (RACSignal *)rac_GET:(NSString *)path parameters:(id)parameters modelClass:(Class)modellClass;

/** post 请求 */
- (RACSignal *)rac_POST:(NSString *)path parameters:(id)parameters modelClass:(Class)modellClass;

- (RACSignal *_Nullable)rac_PUT:(NSString *_Nullable)path parameters:(nullable id)parameters modelClass:(Class)modellClass;

- (RACSignal *_Nullable)rac_DELETE:(NSString *_Nullable)path parameters:(nullable id)parameters modelClass:(Class)modellClass;

/** 文件上传  */
- (RACSignal *_Nullable)rac_POST:(NSString *_Nullable)path
             parameters:(nullable id)parameters
       constructingBodyWithBlock:(void (^_Nullable)(id<AFMultipartFormData> formData))block Progress:(ProgressBlock _Nullable )progressBlock;


//+ (NSMutableDictionary<NSString *, Class> *_Nullable)dictionaryOfEndpointClasses;
// 通过端号获取模型对象
+ (nullable Class)getResponseClass:(NSString *_Nullable)endpoint;
// 设置模型
//+ (void)setResponseClass:(Class _Nonnull )responseClass forEndpoint:(NSString *_Nullable)endpoint;

- (nullable NSObject *)resolveResponse:(NSHTTPURLResponse *_Nullable)response
                              endpoint:(NSString *_Nullable)endpoint
                        responseObject:(id _Nonnull )responseObject;
@end
